﻿using _8Group.MES.SongService;
using _8Group.MES.MeasuringUnit;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Threading.Tasks;
using _8Group.MES.ISongService.MdItemType.DTO;
using _8Group.MES.Service;
using _8Group.MES.Meng_Service;
using _8Group.MES.ClientManagement;
using _8Group.MES.Model.MD主数据;
using _8Group.MES.MeasuringUnit.DTO;
using FourGroups.Project;
using System;
using _8Group.MES.IAppService.Clients.Dto;

namespace _8Group.MES.Web.Controllers
{
    /// <summary>
    /// 主数据控制器
    /// </summary>
    public class MainDataController : Controller
    {
        private readonly MdItemTypeService _mdItemTypeService;
        private readonly MeasuringUnitService _measuringUnitService;
        private readonly MaterialProductService _materialProductService;
        private readonly MdVendorService _mdvendorService;
        private readonly ClientAppServic _mdClientService;
        public MainDataController(MdItemTypeService mdItemTypeService, MeasuringUnitService measuringUnitService, MaterialProductService materialProductService, MdVendorService mdvendorService, ClientAppServic mdClientService)
        {
            _mdItemTypeService = mdItemTypeService;
            _measuringUnitService = measuringUnitService;
            _materialProductService = materialProductService;
            _mdvendorService = mdvendorService;
            _mdClientService = mdClientService;
        }
        /// <summary>
        /// 物料产品管理
        /// </summary>
        /// <returns></returns>
        public IActionResult MaterialProduct()
        {
            return View();
        }
        /// <summary>
        /// 物料产品分类
        /// </summary>
        /// <returns></returns>
        public IActionResult MaterialProductType()
        {
            return View();
        }
       
        public async Task<object> GetMdItems(string? code, string? name, int page, int limit)
        {
            var result = await _materialProductService.GetMdItemsListAsync(code, name, page, limit);
            return result;
        }
        /// <summary>
        /// 供应商管理
        /// </summary>
        /// <param name="vendorCode"></param>
        /// <param name="vendorName"></param>
        /// <param name="vendorNick"></param>
        /// <returns></returns>
        public async Task<object> MdVendorList(string? vendorCode, string? vendorName, string? vendorNick)
        {
            var result = await _mdvendorService.GetMdVendorListAsync(vendorCode, vendorName, vendorNick);
            return result;
        }
        /// <summary>
        /// 计量单位
        /// </summary>
        /// <returns></returns>
        public IActionResult MeasuringUnit()
        {
            return View();
        }

        /// <summary>
        /// 客户管理列表
        /// </summary>
        /// <returns></returns>
        public IActionResult CustomerManagement()
        {
            return View();
        }

        /// <summary>
        /// 添加客户管理
        /// </summary>
        /// <returns></returns>
        public IActionResult AddClientView()
        {
            return View();
        }

        /// <summary>
        /// 修改客户管理
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdateClientView()
        {
            return View();
        }

        /// <summary>
        /// 添加客户
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddClient(CreateClienDto input)
        {
            var result = await _mdClientService.AddClient(input);
            return result;
        }
        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="id">客户编号</param>
        /// <returns></returns>
        public async Task<bool> DeleteClient(int id)
        {
            var client = await _mdClientService.DeleteClient(id);
            return true;
        }

        /// <summary>
        /// 修改客户
        /// </summary>
        /// <param name="input">客户信息</param>
        /// <returns></returns>
        public async Task<bool> UpdateClient(UpdateClienDto input)
        {
            var client = await _mdClientService.UpdateClient(input);
            return true;
        }

        /// <summary>
        /// 客户列表
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetClientsAsync(string? Code, string? Name, string? Nick, int pageIndex = 1, int pageSize = 10)
        {
            var clients = await _mdClientService.GetMdClients(Code, Name, Nick, pageIndex, pageSize);
            return clients;
        }

        /// <summary>
        /// 供应商管理
        /// </summary>
        /// <returns></returns>
        public IActionResult SupplierManagement()
        {
            return View();
        }
        /// <summary>
        /// 车间管理
        /// </summary>
        /// <returns></returns>
        public IActionResult ShopFloor()
        {
            return View();
        }
        /// <summary>
        /// 工作站
        /// </summary>
        /// <returns></returns>
        public IActionResult WorkStation()
        {
            return View();
        }
        public async Task<JsonResult> AddMeasuringUnit(CreateMeasuringUnit input)
        {
            var a = await _measuringUnitService.AddMeasuringUnit(input);
            return Json(a);
        }
        /// <summary>
        /// 反填计量单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IActionResult> UpdateUnitMeasureById(UpdateMeasuringUnitDTO input)
        {
            var measuringUnit = await _measuringUnitService.UpdateUnitMeasureById(input);
            return Json(measuringUnit);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public async Task<JsonResult> DeleteMeasuringUnit(int id)
        {
            var measureUnitList = await _measuringUnitService.DeleteMeasuringUnit(id);
            return Json(measureUnitList);
        }
        /// <summary>
        /// 计量单位显示
        /// </summary>
        /// <param name="measureCode"></param>
        /// <param name="measureName"></param>
        /// <returns></returns>
        public async Task<object> ShowAsyncs(string? measureCode, string? measureName)
        {
            var result = await _measuringUnitService.GetMeasuringUnitAsync(measureCode, measureName);
            return result;
        }

    }
}
